#include <iostream>
#include "SQLError.h"
#include "InsertQuery.h"
#include "DBFile.h"
#include "SQLTable.h"

InsertQuery::InsertQuery(void)
{
}

InsertQuery::InsertQuery(const QueryInfo & q) : SQLQuery(q.type)
{
	this->SetTablename(q.table_name[0]);
	queryInfo = q;
}

RowSet InsertQuery::Execute()
{
	RowSet s;
	SQLTable table(GetTablename());
	try
	{
		RowStructure row;
		row.ConstructData(queryInfo);
		int len = row.GetRecordLen();
		char * r = (char *)malloc(len);
		row.FillInRow(r);

		table.OpenTable();
		table.InsertRecord(len, r);
		s.IncreaseAffected();
		table.CloseTable();
	}
	catch (BaseException & e)
	{
		std::cerr << e.what();
	}
	return s;
}

InsertQuery::~InsertQuery(void)
{
}
